package com.city.dispatch.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.city.dispatch.R;
import com.city.dispatch.core.common.HttpHelper;
import com.city.dispatch.core.common.SharedPreferencesUrls;
import com.city.dispatch.core.common.UIHelper;
import com.city.dispatch.core.common.Urls;
import com.city.dispatch.fragment.BikeUnbindFragment;
import com.city.dispatch.fragment.EbikeUnbindFragment;
import com.city.dispatch.model.CarmodelBean;
import com.city.dispatch.model.ResultConsel;
import com.city.dispatch.model.TabBean;
import com.city.dispatch.model.TabTopEntity;
import com.city.dispatch.swipebacklayout.app.SwipeBackActivity;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.zxing.lib.scaner.activity.ActivityScanerCode;

import org.apache.http.Header;
import org.json.JSONArray;

import java.util.ArrayList;


import static android.content.pm.PackageManager.PERMISSION_GRANTED;

@SuppressLint("NewApi")
public class CarUnbindActivity extends SwipeBackActivity implements View.OnClickListener {

    CommonTabLayout tab;

    private LinearLayout ll_back;
    private TextView title;
    private ImageView rightBtn;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private String[] mTitles = { "助力车", "单车"};

    String title1;
    String title2;
    String phone1;
    String phone2;

    private String bikeCode = "";

    PopupWindow popupwindow;

    EbikeUnbindFragment ebikeUnbindFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_unbind);
        context = this;

        bikeCode = getIntent().getStringExtra("bikeCode");

        init();

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
    }

    private void init(){

        ll_back = (LinearLayout) findViewById(R.id.ll_backBtn);
        title = (TextView) findViewById(R.id.mainUI_title_titleText);
        title.setText("车辆解绑");
        rightBtn = (ImageView)findViewById(R.id.mainUI_title_rightBtn);

        tab = (CommonTabLayout) findViewById(R.id.tab);

        ebikeUnbindFragment = new EbikeUnbindFragment();
//        BikeUnbindFragment bikeUnbindFragment = new BikeUnbindFragment();
        mFragments.add(ebikeUnbindFragment);
//        mFragments.add(bikeUnbindFragment);


        Log.e("cfa===initData", "===");

        for (int i = 0; i < 1; i++) {
            mTabEntities.add(new TabTopEntity(mTitles[i]));
        }

        tab = (CommonTabLayout) findViewById(R.id.tab);
        tab.setTabData(mTabEntities, this, R.id.fl_carFault, mFragments);


        ll_back.setOnClickListener(this);
        rightBtn.setOnClickListener(this);

//        initHttp();
    }

    @Override
    public void onResume() {
        super.onResume();

        ebikeUnbindFragment.resetList();

//        isRefresh = true;
//        if(data.size()!=0){
//            myAdapter.notifyDataSetChanged();
//        }
    }

    JSONArray carmodel_array;
    JSONArray carmodel1_array;
    JSONArray carmodel2_array;
    private void initHttp(){
        String access_token = SharedPreferencesUrls.getInstance().getString("access_token","");
        if (access_token == null || "".equals(access_token)){
            Toast.makeText(context,"请先登录账号",Toast.LENGTH_SHORT).show();
            return;
        }
        RequestParams params = new RequestParams();
        params.put("tab", 1);

        Log.e("cycling_cards===","===");

        HttpHelper.get(context, Urls.untieCar_list, params, new TextHttpResponseHandler() {
            @Override
            public void onStart() {
                onStartCommon("正在加载");
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                onFailureCommon(throwable.toString());
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, final String responseString) {

                m_myHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Log.e("cycling_cards===1","==="+responseString);

                            ResultConsel result = JSON.parseObject(responseString, ResultConsel.class);
                            TabBean obj = JSON.parseObject(result.getData(), TabBean.class);
                            Log.e("cycling_cards===2",obj.getTabs()+"==="+obj.getDatas());

                            carmodel_array = new JSONArray(obj.getTabs());
//                            carmodel_num = carmodel_array.length();
                            Log.e("cycling_cards===3","==="+carmodel_array.length());

                            CarmodelBean obj2 = JSON.parseObject(obj.getDatas(), CarmodelBean.class);
                            Log.e("cycling_cards===4",obj2.getCarmodel1()+"==="+obj2.getCarmodel2());

//                            carmodel1 = obj2.getCarmodel1();
//                            carmodel2 = obj2.getCarmodel2();
//
//                            carmodel1_array = new JSONArray(carmodel1);
//                            carmodel2_array = new JSONArray(carmodel2);
//
//                            myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
//                            vp.setAdapter(myPagerAdapter);
//                            tab.setupWithViewPager(vp);
//
//                            vp.setCurrentItem(0);
//
//                            vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//                                @Override
//                                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//                                }
//
//                                @Override public void onPageSelected(int position) {
//                                    vp.setCurrentItem(position);
//                                }
//
//                                @Override public void onPageScrollStateChanged(int state) {
//
//                                }
//                            });


//                            bikeUnbindFragment.data = carmodel1;
//                            ebikeUnbindFragment.data = carmodel2;
//
//                            Log.e("MyPagerAdapter===3","==="+carmodel_array);
//
//                            try {
//                                fragmentList = new ArrayList<>();
//                                if(carmodel_array.length()==2){
//                                    fragmentList.add(bikeUnbindFragment);
//                                    fragmentList.add(ebikeUnbindFragment);
//                                }else{
//                                    if(carmodel1_array.length()>0){
//                                        fragmentList.add(bikeUnbindFragment);
//                                    }else if(carmodel2_array.length()>0){
//                                        fragmentList.add(ebikeUnbindFragment);
//                                    }else{
//                                        fragmentList.add(bikeUnbindFragment);
//                                    }
//                                }
//
//
//                            } catch (Exception e) {
//                                e.printStackTrace();
//                            }
//
//                            Log.e("MyPagerAdapter===4","==="+carmodel_num);
//
//                            if(carmodel_num<2){
//                                ll_tab.setVisibility(View.GONE);   //TODO
//                                title.setVisibility(View.VISIBLE);
//                                title.setText("套餐卡");
//                            }else{
//                                ll_tab.setVisibility(View.VISIBLE);   //TODO
//                                title.setVisibility(View.GONE);
//                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                        }
                        if (loadingDialog != null && loadingDialog.isShowing()){
                            loadingDialog.dismiss();
                        }

                    }
                });



            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.ll_backBtn:
                scrollToFinishActivity();
                break;
            case R.id.mainUI_title_rightBtn:
//                UIHelper.goToAct(context, MyCartActivity.class);

//                if(popupwindow==null || (popupwindow!=null && !popupwindow.isShowing())){
//                    initmPopupWindowView();
//                }

                Intent intent = new Intent();
                intent.setClass(context, ActivityScanerCode.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("isUnbind",true);
                startActivityForResult(intent, 10);

//                UIHelper.goToAct(context, CarUnbindDetailActivity.class);

                break;
        }
    }


}